"""
get a Directory
只解析一个目标资源URL列表，调取函数
"""

import os, requests, re
from requests.exceptions import *
from HTTP.Downloader1024.common.Logging_Model import *
header = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}


def getHtml(url):
    """
    get a code from a html through url.
    :param url:
    :return: A str about the html.
    """
    global page
    _n = 3
    while _n:
        _n -= 1
        try:
            page = requests.get(url, headers=header, timeout=5)
            page.encoding = "utf-8"
            if page.status_code is 200:
                page = page.text
                break
            else:
                logger.error("网页获取获取错误。")
                if _n is 0: return 'Error'
        except ConnectionError or ConnectionError:
            logger.error("连接错误")
            if _n is 0: return 'Error'
        except TimeoutError or ReadTimeout:
            logger.error("连接超时")
            if _n is 0: return 'Error'
        except:
            logger.error("未知错误")
            Traceback()
            if _n is 0: return 'Error'
    if not os.path.exists('./Support'):
        os.mkdir('./Support')
    with open("./Support/tempHTML.html", mode='w', encoding='utf8')as f:
        f.write(f"{page}")
    return page


def item(text):
    """
    返回DIR中的URL
    :param text:
    :return:
    """
    urlHard = re.findall(r'<base id="headbase" href="(.*?)" />', text)[0]
    urlLast = re.findall(r'<h3> 	<a href="(html_data.*?)" id=".*?">.*?</a></h3>', text)
    listUrl = []
    for url in urlLast:
        urlDir = urlHard + url
        listUrl.append(urlDir)
    if len(listUrl) is 0:
        return 'Error'
    if not os.path.exists('./Support'):
        os.mkdir('./Support')
    with open('./Support/directoryURL.txt', mode='w', encoding='utf8') as f:
        # f.write(f'{name}\n')
        for i in listUrl:
            f.write(f'{i}\n')
    return listUrl


def main(url):
    """
    The function can finish the script all of things.
    Convenient for other script calls.
    :param url: Directory URL
    :return: successful or error
    """
    #
    html = getHtml(url)
    if html is 'Error':
        logger.error(f"Directory HTML文件获取失败。（{url}）")
        return 'Error'
    #
    listUrlDir = item(html)
    if listUrlDir is 'Error':
        logger.error("没有找到合适的URL")
        return 'Error'
    return listUrlDir


if __name__ == '__main__':
    URL = 'http://w2.xggfgczh781.pw/pw/thread-htm-fid-106-page-1.html'
    main(URL)
